<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2018/6/13
 * Time: 11:45
 */

namespace app\common\model;

use Exception;

/**
 * 运势日记模型类
 * Class FortuneNote
 * @package app\common\model
 */
class FortuneNote extends Base
{
    protected $table = 'wechat_backup_07_18_14_43_00.app_fortune_note';

    /**
     * 获取用户的运势笔记记录时间
     * @param $map
     * @return array
     * @throws Exception
     */
    public function getUserNote($map = [])
    {
        return (new \app\common\cachemodel\FortuneNote())->getNoteLists($map['uid'], 0);
    }

    /**
     * 分页获取列表
     * @param $map
     * @return array|mixed
     * @throws Exception
     */
    public function getListPage($map = [])
    {
        return (new \app\common\cachemodel\FortuneNote())->getNotePageAll($map['uid'], 1);
    }

    /**
     * 保存运势笔记
     * @param $map
     * @return bool
     * @throws Exception
     */
    public function saveFortuneNote($map = [])
    {
        return (new \app\common\cachemodel\FortuneNote())->SaveData($map);
    }

    /**
     * @param $id
     * @param $map
     * @return bool|array
     * @throws Exception
     */
    public function UpdateData($id, $map)
    {
        //获取运势日记数据
        $arr = (new \app\common\cachemodel\FortuneNote())->getInfoById($id, ['time', 'rid']);
        if ($arr)
        {
            // 组合返回的数据
            $array = [
                'id' => $id,
                'update_time' => time(),
                'content' => $map['content'],
                'state' => $map['state'],
                'mood' => $map['mood'],
                'from' => [
                    'rid' => $arr['rid'],
                    'time' => $arr['time'],
                    'remark' => '每日运势',
                    'icon_url' => config('fortune.thumb'),
                ],
            ];
            (new \app\common\cachemodel\FortuneNote())->UpdateData($id, $map);
            return $array;
        }
        return false;
    }

    /**
     * @param $id
     * @param $uid
     * @return bool
     * @throws Exception
     */
    public function DelData($id, $uid)
    {
        return (new \app\common\cachemodel\FortuneNote())->DeleteData($id, $uid);
    }
}
